package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.SpeMapper;
import com.bwie.pojo.R;
import com.bwie.pojo.Spe;
import com.bwie.pojo.SpeOption;
import com.bwie.pojo.utile.Pageinfo;
import com.bwie.service.SpeService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author DarkClown
 * @date 2024-07-22 16:12
 * @project_name hight5
 * @parem
 **/
@Service
public class SpeServiceImpl extends ServiceImpl<SpeMapper, Spe> implements SpeService {
    @Autowired
    SpeMapper speMapper;
    @Override
    public R selectList(String keyword) {
        QueryWrapper<Spe> queryWrapper = new QueryWrapper<>();
        if (keyword!=null){
            queryWrapper.lambda().like(Spe::getSpecName,keyword);
        }
        List<Spe> spes = speMapper.selectList(queryWrapper);
        for (Spe spe : spes) {
            List<SpeOption> speOptions = speMapper.selectSpeOption(spe.getId());

            spe.setSpeOptions(speOptions);

        }

        return R.ok(spes);
    }

    @Override
    public R updateBySpe(Spe spe) {
        speMapper.updateById(spe);

        speMapper.deleteByIdToOption(spe.getId());

        speMapper.insertOption(spe.getSpeOptions(),spe.getId());

        return R.ok();
    }

    @Override
    public R addSpe(Spe spe) {
        int insert = speMapper.insert(spe);
        speMapper.insertOption(spe.getSpeOptions(), spe.getId());
        return R.ok();
    }

    @Override
    public R delectByIds(Integer[] ids) {
        for (Integer id : ids) {
            speMapper.deleteByIdToOption(id);
            speMapper.deleteById(id);
        }
        return R.ok();
    }

    @Override
    public R delectByIdToOption(Integer id) {
        Spe spe = new Spe();
        spe.setId(id);
        speMapper.deleteByOption(spe);
         return R.ok();
    }


}
